<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.name" placeholder="请输入设备名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-hasPermi="['admin:electronic:save']">添加设备</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" v-loading="loading" size="mini" @sort-change="handleSortChange" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="number"
          label="序号"
          width="50"
          sortable="custom"
        />
        <el-table-column label="设备名称" prop="name" min-width="120"/>
        <el-table-column label="设备品牌" prop="brandText" min-width="100"/>
        <el-table-column label="设备型号" prop="deviceText" min-width="100"/>
        <el-table-column label="设备类型" prop="deviceTypeText" min-width="100"/>
        <el-table-column label="所属人" prop="personName" min-width="100"/>
<!--        <el-table-column label="机身串码" prop="serialization" min-width="100"/>-->
<!--        <el-table-column label="Mac地址" prop="macAddress" min-width="100"/>-->
        <el-table-column label="启用时间" prop="activationTime" min-width="100"/>
        <el-table-column label="停用时间" prop="downTime" min-width="100"/>
        <el-table-column label="操作" min-width="130"  fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:electronic:info']">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:electronic:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '创建设备':'编辑设备'"
      destroy-on-close
      :close-on-click-modal="false"
      width="700px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :all-category-data="categoryData"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as electronicApi from '@/api/electronic.js'
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission";
import * as categoryApi from "@/api/categoryApi";
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        name: '',
        /*brand: null,
        device: null,
        deviceType: null,
        serialization: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },
      categoryData:{},
    }
  },
  mounted() {
    this.handleGetElectronicList()
    this.initCategoryData()
  },
  methods: {
    handleSortChange(c) {
      const order = c.order;
      if (order == null || order == 'ascending') {
        this.listPram.sortType = 2;
      } else {
        this.listPram.sortType = 1;
      }
      this.handleGetElectronicList();
    },
    checkPermi,
    handleSearch() {
      this.listPram.page = 1
      this.handleGetElectronicList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetElectronicList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetElectronicList()
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        electronicApi.elcDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetElectronicList()
        })
      })
    },
    initCategoryData(){
      this.loading = true

      categoryApi.categoryElcList().then(data => {
        this.categoryData = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },
    handleGetElectronicList() {
      electronicApi.elcList( this.listPram ).then(data => {
        this.listData = data
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetElectronicList()
      this.initCategoryData()
    },
    resetQuery(){
      this.listPram.name = '';
      this.handleGetElectronicList()
    }
  }
}
</script>

<style scoped>

</style>
